/**
 * @author Juko
 */
package ganga.custodian.dashboard;

import ganga.custodian.CustodianApplication;
import ganga.custodian.R;
import ganga.custodian.plot.BMPSeries;
import ganga.custodian.plot.KIGSeries;

import java.text.DecimalFormat;
import java.util.Vector;

import android.graphics.Color;

import com.androidplot.ui.layout.DynamicTableModel;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.LineAndPointRenderer;
import com.androidplot.xy.XYPlot;

public class MonitorScreenPlotData {
	public XYPlot kigPlot;
	public XYPlot bpmPlot;
	public BMPSeries dinamicBPM;
	public KIGSeries dinamicKIG;
	public LineAndPointFormatter colorKIGSeries;
	public LineAndPointFormatter colorBPMSeries;
	public Vector<Double> dinamicVectorKIG;
	public Vector<Double> dinamicVectorBPM;
	public boolean firstBeatBPM;
	public boolean firstBeatKIG;
	
	public MonitorScreenPlotData(Vector<Double> dinamicVectorBPM ,Vector<Double> dinamicVectorKIG ) {
		this.dinamicVectorBPM = dinamicVectorBPM;
		this.dinamicVectorKIG=dinamicVectorKIG;
		this.firstBeatBPM = true;
		this.firstBeatKIG = true;
	}

	public void buildXYPlot(MonitorScreenActivity monitorScreen, XYPlot aSimpleXYPlot) {

		aSimpleXYPlot.getGraphWidget().setTicksPerRangeLabel(2);
		aSimpleXYPlot.getGraphWidget().setTicksPerDomainLabel(5);
		aSimpleXYPlot.getGraphWidget().setRangeValueFormat(
				new DecimalFormat("#####"));
		aSimpleXYPlot.getGraphWidget().setDomainValueFormat(
				new DecimalFormat("#####"));
		aSimpleXYPlot.getGraphWidget().setRangeLabelWidth(25);
		aSimpleXYPlot.setDomainLabel(" ");
		aSimpleXYPlot.disableAllMarkup();
		colorBPMSeries = new LineAndPointFormatter(Color.rgb(0, 155, 255),
				Color.rgb(80, 0, 255), null);
		colorKIGSeries = new LineAndPointFormatter(Color.rgb(255, 200, 0),
				Color.rgb(200, 255, 0), null);
		aSimpleXYPlot.getLegendWidget().setTableModel(
				new DynamicTableModel(0, 0));
		kigPlot.setRangeLabel(CustodianApplication.getInstance().getString(
				R.string.RRInterval));
		bpmPlot.setRangeLabel(CustodianApplication.getInstance().getString(
				R.string.BeatsPerMinute));
		dinamicBPM = new BMPSeries(dinamicVectorBPM);
		dinamicKIG = new KIGSeries(dinamicVectorKIG);
		dinamicKIG.setLabel(CustodianApplication.getInstance().getString(
				R.string.KIG));
		dinamicBPM.setLabel(CustodianApplication.getInstance().getString(
				R.string.BMP));
	}

	/**
	 * Функция перерисовки графика
	 * 
	 * @param monitorScreen
	 *            TODO
	 * @param interval
	 *            Новый РР интервал в милисекундах
	 */
	public void setNewInterval(MonitorScreenActivity monitorScreen, double interval) {

		if (firstBeatKIG == false) {
			if (dinamicVectorKIG.size() > 15)
				dinamicVectorKIG.remove(0);
			dinamicVectorKIG.add(interval);
			kigPlot.addSeries(dinamicKIG, LineAndPointRenderer.class,
					colorKIGSeries);
			kigPlot.redraw();
		} else {
			firstBeatKIG = false;
		}
	}
	public void setNewHeartRate(MonitorScreenActivity monitorScreen, double heartRate) {
		if (firstBeatBPM == false) {
			if (dinamicVectorBPM.size() > 15)
				dinamicVectorBPM.remove(0);
			dinamicVectorBPM.add(heartRate);
			bpmPlot.addSeries(dinamicBPM, LineAndPointRenderer.class,
					colorBPMSeries);
			bpmPlot.redraw();
		} else {
			firstBeatBPM = false;
		}
	}

}